package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.template.TravelTemplate;
import cn.wolfcode.luowowo.search.template.UserInfoTemplate;
import cn.wolfcode.luowowo.search.vo.SearchResult;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class SearchController {

    @Reference
    private IDestinationSearchService destinationSearchService;

    @Reference
    private IStrategySearchService strategySearchService;

    @Reference
    private ITravelSearchService travelSearchService;

    @Reference
    private IUserInfoSearchService userInfoSearchService;

    @Reference
    private ISearchService searchService;

    @RequestMapping("/q")
    public Object search(@ModelAttribute("qo") SearchQueryObject qo, Model model) {
        //区分是查询什么:全部/目的地/攻略/游记/找人
        String viewName = null;
        switch (qo.getType()) {
            case SearchQueryObject.SEARCH_ALL:      //-1:
                searchAll(qo, model);
                viewName = "searchAll";
                break;
            case SearchQueryObject.SEARCH_DEST:     //0:
                searchDest(qo, model);
                viewName = "searchDest";
                break;
            case SearchQueryObject.SEARCH_STRATEGY: //1
                searchStrategy(qo, model);
                viewName = "searchStrategy";
                break;
            case SearchQueryObject.SEARCH_TRAVEL:   //2
                searchTravel(qo, model);
                viewName = "searchTravel";
                break;
            case SearchQueryObject.SEARCH_USER:     //3
                searchUserInfo(qo, model);
                viewName = "searchUser";
                break;
        }
        return "index/" + viewName;
    }

    /**
     * 站内搜索之目的地查询
     *
     * @param qo
     * @param model
     */
    private void searchDest(SearchQueryObject qo, Model model) {
        //添加总数
        Integer total = 0;

        SearchResult data = new SearchResult();

        //dest 查询的当前目的地
        DestinationTemplate dest = destinationSearchService.getByName(qo.getKeyword());
        model.addAttribute("dest", dest);
        if (dest != null) {
            total = 1;

            //data 当前搜索的结果
            // 攻略
            List<StrategyTemplate> sts = strategySearchService.listByDestName(qo.getKeyword());
            total += sts.size();
            //游记
            List<TravelTemplate> tts = travelSearchService.listByDestName(qo.getKeyword());
            total += tts.size();
            //用户
            List<UserInfoTemplate> uts = userInfoSearchService.listByCity(qo.getKeyword());
            total += uts.size();

            //把数据添加到data里面
            data.setStrategys(sts);
            data.setTravels(tts);
            data.setUsers(uts);
            data.setTotal(total);
        }
        //共享data
        model.addAttribute("data", data);
    }

    /**
     * 站内搜索之全部
     *
     * @param qo
     * @param model
     */
    private void searchAll(SearchQueryObject qo, Model model) {
        int total = 0;
        //data 当前搜索的内容
        Page<DestinationTemplate> destination = searchService.hightLightSearch(DestinationTemplate.INDEX_TYPE_NAME,
                DestinationTemplate.class, qo, "name", "info");
        total += destination.getTotalElements();
        Page<StrategyTemplate> strategy = searchService.hightLightSearch(StrategyTemplate.INDEX_TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle", "summary");
        total += strategy.getTotalElements();
        Page<TravelTemplate> travel = searchService.hightLightSearch(TravelTemplate.INDEX_TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");
        total += travel.getTotalElements();
        Page<UserInfoTemplate> userInfo = searchService.hightLightSearch(UserInfoTemplate.INDEX_TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "city");
        total += userInfo.getTotalElements();

        SearchResult data = new SearchResult();
        data.setDests(destination.getContent());
        data.setStrategys(strategy.getContent());
        data.setTravels(travel.getContent());
        data.setUsers(userInfo.getContent());
        data.setTotal(total);
        model.addAttribute("data", data);
    }

    /**
     * 站内搜索之攻略
     *
     * @param qo
     * @param model
     */
    private void searchStrategy(SearchQueryObject qo, Model model) {
        Page<StrategyTemplate> page = searchService.hightLightSearch(StrategyTemplate.INDEX_TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle", "summary");
        model.addAttribute("page", page);
    }

    /**
     * 站内搜索之游记
     *
     * @param qo
     * @param model
     */
    private void searchTravel(SearchQueryObject qo, Model model) {
        Page<TravelTemplate> page = searchService.hightLightSearch(TravelTemplate.INDEX_TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");
        model.addAttribute("page", page);
    }

    /**
     * 站内搜索之找人
     *
     * @param qo
     * @param model
     */
    private void searchUserInfo(SearchQueryObject qo, Model model) {
        Page<UserInfoTemplate> page = searchService.hightLightSearch(UserInfoTemplate.INDEX_TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "city");
        model.addAttribute("page", page);
    }

}
